'''
请求对象定制===UA反爬
'''

import http
from urllib import request
from urllib.request import Request

from urllib3 import HTTPResponse

url = "http://www.weather.com.cn/weather1d/101010100.shtml"

headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36 SLBrowser/9.0.3.5211 SLBChan/105"
}

# 请求对象定制
my_request: Request = request.Request(url=url, headers=headers)
response: HTTPResponse = request.urlopen(my_request)
print(response.getcode())  # 获取状态码
print(response.getheaders())  # 获取响应头 [('Date', 'Fri, 14 Jun 2024 08:08:21 GMT'), ('Content-Type', 'text/html'), ... ]

# 返回字节形式（一个字节一个字节去读取）的二进制数据, 需要二进制转换为字符串
# response.read(10) 读取10个字节
# content_by_byte = response.read().decode("utf-8")
# print(content_by_byte)

# # 读取一行
# content_by_line = response.readline().decode("utf-8")
# print(content_by_line)


# 一行一行去读，直到读完
content_by_alllines = response.readlines()
for line in content_by_alllines:
    line = line.decode("utf-8")
    print(line)
